// pages/wode/wode.js
const app=getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
    data: {
        url: 'http://smyl.qingkangqingyang.com/api/miaozhimini/weixin/fileUpload',
        showaddress: '请选择',
        province: '',
        provinceCode: '',
        cityCode: '',
        areaCode: '',
        businessTime: '',
        description: '',
        lon: '',
        lat: '',
        imgSrc: '../../images/add_img.png',
        imgsSrc: [],
        businessImg: '../../images/add_img.png',
        bankOpName: '',
        bankNumber: '',
        bankName: '',
        aliAccount: '',
        idCardNum: '',
        contactName: '',
        contactPhone: '',
        pnum: '',
        address: '请选择',
        title: '',
        userinfo: [],
        has: 0,
        status: 0,
        remark: '',
        id: 0,
        region: ["请选择地址",'','']
    },

    onLoad() {
        this.setData({
            userinfo: wx.getStorageSync('user')
        });
        this.getinfo();
    },

    showPicker(e) {
        const type = e.currentTarget.dataset.type;
        this.selectComponent(`#${type}`).show();
    },
    bindRegionChange: function (e) {
        var value = e.detail.value; 
        var code = e.detail.code; 
        var postcode = e.detail.postcode;
        this.setData({
            region: e.detail.value,
            province: value[0],
            provinceCode: code[0],
            city: value[1],
            cityCode: code[1],
            area: value[2],
            areaCode: code[2],
        })
    },
    getinfo() {
        const that = this;
        var userid=app.globalData.userId;
        util.httpRrquest(app.data.apiurl + 'shop/getMyshop', 'GET', {})
        .then(function (response) {
            const data = response.data.data;
            if (data.has == 1) {
                that.setData({
                    title: data.data.title,
                    contactPhone: data.data.contactPhone,
                    address: data.data.address,
                    showaddress: data.data.province + data.data.city + data.data.area,
                    bankOpName: data.data.bankOpName,
                    bankNumber: data.data.bankNumber,
                    pnum: data.data.topshopnum,
                    bankName: data.data.bankName,
                    aliAccount: data.data.aliAccount,
                    idCardNum: data.data.idCardNum,
                    contactName: data.data.contactName,
                    imgSrc: data.data.imgSrc,
                    imgsSrc: data.data.imgsSrc,
                    businessImg: data.data.businessImg,
                    status: data.data.status,
                    province: data.data.province,
                    provinceCode: data.data.provinceCode,
                    city: data.data.city,
                    cityCode: data.data.cityCode,
                    area: data.data.area,
                    areaCode: data.data.areaCode,
                    description: data.data.description,
                    businessTime: data.data.businessTime,
                    remark: data.data.remark ? data.data.remark : '无',
                    id: data.data.id
                });
            }
        }, function (error) {
            console.log(error);
        })
    },

    chooseImage(e) {
        const type = e.currentTarget.dataset.type;
        const that = this;
        wx.chooseImage({
            count: 6,
            sourceType: ['album'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                that.setData({
                    user_avatar: res.tempFilePaths[0]
                });
                wx.showLoading({
                    title: '上传中',
                });
                wx.uploadFile({
                    url: that.data.url,
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    formData: {},
                    success(uploadFileRes) {
                        wx.hideLoading();
                        const data = JSON.parse(uploadFileRes.data).data;
                        if (type == 1) {
                            that.setData({
                                imgSrc: data
                            });
                        } else if (type == 2) {
                            that.setData({
                                imgsSrc: that.data.imgsSrc.concat(data)
                            });
                        } else if (type == 3) {
                            that.setData({
                                businessImg: data
                            });
                        }
                    }
                });
            }
        });
    },

    del(e) {
        const index = e.currentTarget.dataset.index;
        const imgsSrc = this.data.imgsSrc;
        imgsSrc.splice(index, 1);
        this.setData({
            imgsSrc: imgsSrc
        });
    },

    chooseLocation() {
    	console.log(2);
        const that = this;
        wx.chooseLocation({
            success(res) {
                that.setData({
                    address: res.address + "-" + res.name,
                    lat: res.latitude,
                    lon: res.longitude
                });
            }
        });
    },

    subinfo() {
        const that = this;
        if (!this.data.address) {
            wx.showToast({
                icon: 'none',
                title: '请选择地址'
            });
            return;
        }

        if (!this.data.title) {
            wx.showToast({
                icon: 'none',
                title: '请填写体验店名称'
            });
            return;
        }

        if (!this.data.contactName) {
            wx.showToast({
                icon: 'none',
                title: '请填写联系人'
            });
            return;
        }

        if (!this.data.contactPhone) {
            wx.showToast({
                icon: 'none',
                title: '请填写联系电话'
            });
            return;
        }

        if (!this.data.bankOpName) {
            wx.showToast({
                icon: 'none',
                title: '请填写开户行'
            });
            return;
        }

        if (!this.data.bankName) {
            wx.showToast({
                icon: 'none',
                title: '请填写银行名称'
            });
            return;
        }

        if (!this.data.bankNumber) {
            wx.showToast({
                icon: 'none',
                title: '请填写银行卡号'
            });
            return;
        }

        if (!this.data.aliAccount) {
            wx.showToast({
                icon: 'none',
                title: '请填写支付宝账号'
            });
            return;
        }

        if (!this.data.businessTime) {
            wx.showToast({
                icon: 'none',
                title: '请填写营业时间'
            });
            return;
        }

        if (!this.data.description) {
            wx.showToast({
                icon: 'none',
                title: '请填写店铺简介'
            });
            return;
        }

        if (!this.data.idCardNum) {
            wx.showToast({
                icon: 'none',
                title: '请填写身份证号'
            });
            return;
        }

        if (!this.data.pnum) {
            wx.showToast({
                icon: 'none',
                title: '请填写推荐店铺'
            });
            return;
        }

        if (!this.data.imgSrc) {
            wx.showToast({
                icon: 'none',
                title: '请上传门头照片'
            });
            return;
        }

        if (!this.data.imgsSrc.length) {
            wx.showToast({
                icon: 'none',
                title: '请上传店铺内照片'
            });
            return;
        }

        if (!this.data.businessImg) {
            wx.showToast({
                icon: 'none',
                title: '请上传营业执照'
            });
            return;
        }

        const imgsSrcstr = this.data.imgsSrc.join('|');
        wx.showModal({
            title: '提示',
            content: '确认提交申请吗？',
            success(res) {
                if (res.confirm) {
                	var postdata = {
			            address: that.data.address,
                        lon: that.data.lon,
                        lat: that.data.lat,
                        imgSrc: that.data.imgSrc,
                        businessImg: that.data.businessImg,
                        bankOpName: that.data.bankOpName,
                        bankNumber: that.data.bankNumber,
                        bankName: that.data.bankName,
                        aliAccount: that.data.aliAccount,
                        idCardNum: that.data.idCardNum,
                        contactName: that.data.contactName,
                        pnum: that.data.pnum,
                        title: that.data.title,
                        contactPhone: that.data.contactPhone,
                        imgsSrc: imgsSrcstr,
                        province: that.data.province,
                        provinceCode: that.data.provinceCode,
                        city: that.data.city,
                        cityCode: that.data.cityCode,
                        area: that.data.area,
                        areaCode: that.data.areaCode,
                        businessTime: that.data.businessTime,
                        description: that.data.description
			        }
			        util.httpRrquest(app.data.apiurl + 'shop/addShopApply', 'POST', postdata)
		            .then(function (response) {
		            	if (response.data.code == -1) {
                            wx.showToast({
                                icon: 'none',
                                title: response.data.msg
                            });
                        } else {
                            wx.showToast({
                                icon: 'none',
                                title: '提交成功，请耐心等待审核！'
                            });
                            setTimeout(() => {
                                wx.navigateBack();
                            }, 1500);
                        }
		                
		            }, function (error) {
		                console.log(error);
		            });
                }
            }
        });
    },

    houtai() {
        wx.navigateTo({
            url: '/pages/shopAdmin/shopAdmin?id='+this.data.id
        });
    },

    bindTitleInput(e) {
        this.setData({
            title: e.detail.value
        });
    },

    bindBusinessTimeInput(e) {
        this.setData({
            businessTime: e.detail.value
        });
    },

    bindDescriptionInput(e) {
        this.setData({
            description: e.detail.value
        });
    },

    bindPnumInput(e) {
        this.setData({
            pnum: e.detail.value
        });
    },

    bindContactNameInput(e) {
        this.setData({
            contactName: e.detail.value
        });
    },

    bindContactPhoneInput(e) {
        this.setData({
            contactPhone: e.detail.value
        });
    },

    bindIdCardNumInput(e) {
        this.setData({
            idCardNum: e.detail.value
        });
    },

    bindAliAccountInput(e) {
        this.setData({
            aliAccount: e.detail.value
        });
    },

    bindBankNameInput(e) {
        this.setData({
            bankName: e.detail.value
        });
    },

    bindBankNumberInput(e) {
        this.setData({
            bankNumber: e.detail.value
        });
    },

    bindBankOpNameInput(e) {
        this.setData({
            bankOpName: e.detail.value
        });
    }
});